﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lox_z12Class
{
    class LoxClass : LoxCallable
    {
        public string name;
        private Dictionary<String, LoxFunction> methods;

        public LoxClass(String name, Dictionary<String, LoxFunction> methods)
        {
            this.name = name;
            this.methods = methods;
        }
        public LoxFunction findMethod(String name)
        {
            if (methods.ContainsKey(name)) {
                return methods[name];
            }

            return null;
        }

        public string toString() { return name; }
        public Object call(Interpreter interpreter, List<Object> arguments)
        {
            LoxInstance instance = new LoxInstance(this);
            LoxFunction initializer = findMethod("init");
            if (initializer != null) {
                initializer.bind(instance).call(interpreter, arguments);
            }

            return instance;
        }

        public int arity()
        {
            LoxFunction initializer = findMethod("init");
            if (initializer == null)
                return 0;
            return initializer.arity();
        }
    }
}
